﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TpLogger;

namespace TpData.Layer {
    internal class LogDataLayer : BaseSqlDataLayer{

        #region Members

        private const string saveLogStatement = @"log.SaveLog";
        private const string loadLogStatement = @"log.GetLogs";

        #endregion

        #region Load Save Data


        public virtual void SaveLog(int userID, string logType, string logArea, string logDetails, bool isError) {
            SaveLog(userID, logType, logArea, logDetails, isError, new DateTime(1900,1,1));
        }

        public virtual void SaveLog(int userID, string logType, string logArea, string logDetails, bool isError, DateTime logDate) {
            SqlParameter[] parameters = GetParameters(userID, logType, logArea, logDetails, isError, logDate);
            int ret = ExecuteStoredProcedure(saveLogStatement, parameters);
            if (ret == 0) {
                //log.Add("SaveLog: ExecuteStoredProcedure = 0", true);
            }
        }

        #endregion

        #region Functions

        protected virtual SqlParameter[] GetParameters(int userID, string logType, string logArea, string logDetails, bool isError, DateTime logDate) {
            SqlParameter[] ps = new SqlParameter[7];

            ps[0] = new SqlParameter("@UserID", userID);
            ps[1] = new SqlParameter("@LogType", logType);
            ps[2] = new SqlParameter("@LogArea", logArea);
            ps[3] = new SqlParameter("@LogDetails", logDetails);
            ps[4] = new SqlParameter("@IsError", isError);
            ps[5] = new SqlParameter("@ComputerName", Environment.MachineName);
            ps[6] = new SqlParameter("@DllVersion", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
            if (logDate != new DateTime(1900, 1, 1)) {
                ps[7] = new SqlParameter("@LogDateTime", logDate);
            }
            
            return ps;

        }

        #endregion

    }
}
